//
//  GourmentThirdPart.m
//  Gourment
//
//  Created by lanou3g on 15/12/9.
//  Copyright © 2015年 DGY. All rights reserved.
//

#import "GourmentThirdPart.h"

@implementation GourmentThirdPart


/**
 *  单例
 */
+(instancetype)shareGourment{
    static GourmentThirdPart *gourment = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        gourment = [GourmentThirdPart new];
    });
    return gourment;
}



/**
 *  Gourment--根据shop——id获取下团单列表
 */

-(void)shopID:(NSString *)shopId{
    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/shopdeals";
    NSString *httpArg = [NSString stringWithFormat:@"shop_id=%@",shopId];
    [self request: httpUrl withHttpArg: httpArg];
}

/**
 *  Gourment--根据shop_id获取商户详情
 */
-(void)shopid:(NSNumber *)shopid{
    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/shopinfo";
    NSString *httpArg = [NSString stringWithFormat:@"shop_id = %@",shopid];
    [self request: httpUrl withHttpArg: httpArg];
}

/**
 *  Gourment--根据团单id获取商户详情
 */
-(void)dealID:(NSNumber *)dealID{
    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/dealdetail";
    NSString *httpArg = [NSString stringWithFormat:@"deal_id = %@",dealID];
    [self request: httpUrl withHttpArg: httpArg];}

/**
 *  Gourment--获取城市接口
 */
-(void)getCityInfo{
    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/cities";
    NSString *httpArg = @"";
   [self request: httpUrl withHttpArg: httpArg];

}

/**
 *  Gourment--根据城市id获取行政区接口
 */
-(void)cityID:(NSNumber *)cityID{
    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/districts";
    NSString *httpArg = [NSString stringWithFormat:@"city_id = %@",cityID];
    [self request: httpUrl withHttpArg: httpArg];
}

/**
 *  获取团单分类信息
 */
-(void)getCategorues{
    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/categories";
    NSString *httpArg = @"";
    [self request: httpUrl withHttpArg: httpArg];
}


/**
 *  根据城市id和团单id来查询相应的商户
 */
-(void)cityID:(NSNumber *)cityID withDealID:(NSNumber *)dealID{
    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/dealshops";
    NSString *httpArg = [NSString stringWithFormat:@"city_id = %@&deal_id = %@",cityID,dealID];
    [self request: httpUrl withHttpArg: httpArg];
}


/**
 *  Gourment--根据城市id，二级分类id，行政区id，商圈id，用户所在位置（WGS84坐标）,关键词..来查询相应的团单
 */
-(void)cityID:(NSNumber *)cityID withSubcatIDs:(NSString *)subcatIDs withDistrictIDs:(NSString *)districtIDs withBizareaIDs:(NSString *)bizareaIDs withLocation:(NSString *)location withKeyword:(NSString *)keyword{
    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/searchdeals";
    NSString *httpArg = [NSString stringWithFormat:@"city_id=%@&cat_ids=326&subcat_ids=%@&district_ids=%@&bizarea_ids=%@&location=%@&keyword=%@&radius=3000&sort=0&page=1&page_size=10&is_reservation_required=0",cityID,subcatIDs,districtIDs,bizareaIDs,location,keyword];
    [self request: httpUrl withHttpArg: httpArg];
}

/**
 *  Gourment--根据城市id，二级分类的id，行政区id，商圈id，坐标，关键词等查询条件获取商户信息
 */
-(void)cityid:(NSNumber *)cityID withSubcatID:(NSString *)subcatID withDistrictIDs:(NSString *)districtIDs withBizareaIDs:(NSString *)bizareaIDs withLocation:(NSString *)location withKeyword:(NSString *)keyword{
    
    NSString *httpUrl = @"http://apis.baidu.com/baidunuomi/openapi/searchshops";
    NSString *httpArg = [NSString stringWithFormat:@"city_id=%@&cat_ids=326&subcat_ids=%@&district_ids=%@&bizarea_ids=%@&location=%@&keyword=%@&radius=3000&sort=0&page=1&page_size=10&is_reservation_required=0",cityID,subcatID,districtIDs,bizareaIDs,location,keyword];
    [self request: httpUrl withHttpArg: httpArg];

}

-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg  {
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"942daefd507bdc4b2e56cacadab107ab" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                               } else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   NSLog(@"HttpResponseBody %@",responseString);
                               }
                           }];
}


//-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg  {
//    NSString *urlStr = [[NSString alloc]initWithFormat:@"%@?%@",httpUrl,HttpArg];
//    NSURL *url = [NSURL URLWithString:urlStr];
//    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
//    [request setHTTPMethod:@"GET"];
//    [request addValue:@"942daefd507bdc4b2e56cacadab107ab" forHTTPHeaderField:@"apikey"];
//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//
//            NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
//            NSString *responseString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//            NSLog(@"HttpResponseCode:%ld",responseCode);
//            NSLog(@"HttpRespomseBody:%@",responseString);
//    }];
//}
@end
